﻿using LayUI.Web.Models;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.UI.WebControls;

namespace LayUI.Web.Controllers
{
    /// <summary>
    /// https://layuion.com/
    /// 
    /// 雪花儿飘飘，北风那个吹。。。
    /// 
    /// 
    /// 
    /// 
    /// </summary>
    public class PermissionController : Controller
    {
        /// <summary>
        /// 权限管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult AddTest()
        {
            return View();
        }
        /// <summary>
        /// 分页
        /// </summary>
        /// <param name="page">固定值，不能修改</param>
        /// <param name="limit">固定值，不能修改</param>
        /// <param name="name"></param>
        /// <returns></returns>
        public JsonResult GetUserList(int page = 1, int limit = 3, string name = "")
        {
            var studentList = new List<Student> {
                new Student{Id=1,UserName="111", Experience = 1,Sex=1,Score=90,City="西安",Sign="hello"}
                ,new Student{Id=2,UserName="22", Experience =2,Sex=1,Score=90,City="西安",Sign="hello"}
                ,new Student{Id=3,UserName="33", Experience =3,Sex=1,Score=90,City="西安",Sign="hello"}
                ,new Student{Id=4,UserName="44", Experience =4,Sex=2,Score=90,City="西安",Sign="hello"}
                ,new Student{Id=5,UserName="55", Experience =5,Sex=1,Score=90,City="西安",Sign="hello"}
                ,new Student{Id=6,UserName="66", Experience =6,Sex=2,Score=90,City="西安",Sign="hello"}
                ,new Student{Id=7,UserName="77", Experience =7,Sex=2,Score=90,City="西安",Sign="hello"}
                ,new Student{Id=8,UserName="88", Experience =8,Sex=1,Score=90,City="西安",Sign="hello"}
                ,new Student{Id=9,UserName="99", Experience =9,Sex=2,Score=90,City="西安",Sign="hello"}
            };
            var result = studentList.Where(m => m.UserName.Contains(name))
                .Skip((page - 1) * limit)
                .Take(limit)
                .ToList();
            return Json(new { data = result, count = 5, code = "0", msg = "" }, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public JsonResult GetTreeData(int roleId = 1, int id = 1, int t = 1)
        {
            var menus = Test();
            return Json(menus);
        }


        /// <summary>
        /// 添加权限
        /// </summary>
        /// <returns></returns>
        public ActionResult PermissionAdd()
        {
            return View();
        }

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>

        public bool DoPermissionAdd(PermissionInput permissionInput)
        {
            var permission = permissionInput;
            return permission != null;
        }

        /// <summary>
        /// 编辑权限
        /// </summary>
        /// <returns></returns>
        public ActionResult PermissionEdit(int id)
        {
            ViewBag.Id = id;
            return View();
        }

        /// <summary>
        /// 角色管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Role()
        {
            return View();
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleAdd()
        {
            return View();
        }

        /// <summary>
        /// 编辑角色
        /// </summary>
        /// <returns></returns>
        public ActionResult RoleEdit()
        {
            return View();
        }

        /// <summary>
        /// 用户管理
        /// </summary>
        /// <returns></returns>
        public ActionResult Person()
        {
            return View();
        }

        /// <summary>
        /// 添加用户
        /// </summary>
        /// <returns></returns>
        public ActionResult PersonAdd()
        {
            return View();
        }

        /// <summary>
        /// 编辑用户
        /// </summary>
        /// <returns></returns>
        public ActionResult PersonEdit()
        {
            return View();
        }

        public List<TreeNode> Test()
        {
            //模拟从数据库取数据集合
            var list = GetTreeNodes();
            var tmpList = list.Where(m => m.pId == 0).ToList();

            var treeNodes = new List<TreeNode>();
            for (int i = 0; i < tmpList.Count; i++)
            {
                var treeNode = new TreeNode();
                treeNode.id = tmpList[i].id;
                treeNode.title = tmpList[i].title;
                treeNode.children = GetChildrens(list, treeNode.id);
                treeNodes.Add(treeNode);
            }
            return treeNodes;
        }

        /// <summary>
        /// 递归算法  : 指在当前方法内调用自己的这种现象。
        /// </summary>
        /// <param name="permissions"></param>
        /// <param name="pId"></param>
        /// <returns></returns>
        public List<TreeNode> GetChildrens(List<TreeNode> permissions, int pId)
        {
            var treeNodes = new List<TreeNode>();
            var tmpList = permissions.Where(m => m.pId == pId);
            if (!tmpList.Any()) return treeNodes;

            var list = tmpList.ToList();
            for (int i = 0; i < list.Count; i++)
            {
                var treeNode = new TreeNode();
                treeNode.id = list[i].id;
                treeNode.title = list[i].title;
                treeNode.children = GetChildrens(permissions, treeNode.id);
                treeNodes.Add(treeNode);
            }

            return treeNodes;
        }

        public List<TreeNode> GetTreeNodes()
        {
            //5表联查，取出当前用户对应的角色-所分配的权限集合
            return new List<TreeNode>
            {
                new TreeNode{ id=1,pId=0,title="系统管理"},
                new TreeNode{ id=2,pId=0,title="基础数据"},
                new TreeNode{ id=3,pId=1,title="权限管理"},
                new TreeNode{ id=4,pId=1,title="角色管理"},
                new TreeNode{ id=5,pId=1,title="用户管理"},
                new TreeNode{ id=6,pId=2,title="公共字典"},
                new TreeNode{ id=7,pId=2,title="日志管理"}
            };
        }
    }

    public class PermissionInput
    {
        /// <summary>
        /// 主键ID
        /// </summary>
        public int Id { get; set; }

        /// <summary>
        /// 权限名称
        /// </summary>
        public string PermissionName { get; set; }

        /// <summary>
        /// 权限状态
        /// </summary>
        public bool State { get; set; }
    }

    public class TreeNode
    {
        public int id { get; set; }
        public int pId { get; set; }
        public string title { get; set; }
        public string field { get; set; }

        public bool spread => true;

        public List<TreeNode> children { get; set; }
    }
}